﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VK.Comm;
using VK.Casemeet.Lines;
using VK.Comm.TransactSql;
using System.Data;
using VK.Casemeet.Employees;

namespace VK.Casemeet.Web.Admin.Lines
{
    public partial class LineList : System.Web.UI.Page
    {
        public int area1, area2;
        public string display = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            (this.Master as Site).SiteMapPathControl.MapPathList = new List<MapPath>(){
                new MapPath{ Text="桌面",Url="/Admin/Home.aspx"},
                new MapPath{ Text="线路列表",Url="#"}
            };

            if (!IsPostBack)
            {
                //LinkHelper.BindTypeDDL(ddlType, OptionType.Search);
                LoadData(false);
            }
        }

        protected void LoadData(bool isCondition)
        {
            IList<Condition> conditionlst = new List<Condition>();
            if (EmployeeService.GetSessionEmployee().ProvinceID != 0)
            {
                display = "display:none;";
                conditionlst.Add(new Condition { TableName = "Shop", FieldName = "ProvinceID", ConditionType = ConditionType.NumeralEqual, ConditionValue = EmployeeService.GetSessionEmployee().ProvinceID, DbType = DbType.Int32 });
                conditionlst.Add(new Condition { TableName = "Shop", FieldName = "CityID", ConditionType = ConditionType.NumeralEqual, ConditionValue = EmployeeService.GetSessionEmployee().CityID, DbType = DbType.Int32 });
            }
            else
            {
                conditionlst.Add(new Condition { TableName = "Shop", FieldName = "ProvinceID", ConditionType = ConditionType.NumeralEqual, ConditionValue = Util.GetFormInt("ddlArea1"), DbType = DbType.String });
                conditionlst.Add(new Condition { TableName = "Shop", FieldName = "CityID", ConditionType = ConditionType.NumeralEqual, ConditionValue = Util.GetFormInt("ddlArea2"), DbType = DbType.String });
            }

            //conditionlst.Add(new Condition { TableName = "Link", FieldName = "Type", ConditionType = ConditionType.NumeralEqual, ConditionValue = ddlType.SelectedValue, DbType = DbType.Int32 });
            conditionlst.Add(new Condition { TableName = "Line", FieldName = "Name", ConditionType = ConditionType.TextLIKE, ConditionValue = txtName.Text.Trim(), DbType = DbType.String });
            conditionlst.Add(new Condition { TableName = "Shop", FieldName = "Name", ConditionType = ConditionType.TextLIKE, ConditionValue = txtShopName.Text.Trim(), DbType = DbType.String });
            aspNetPager.RecordCount = LineBLL.GetLineCount(conditionlst);
            rptLine.DataSource = LineBLL.GetLineList(aspNetPager.StartRecordIndex, aspNetPager.EndRecordIndex, conditionlst, string.Empty, SortMode.Desc);
            rptLine.DataBind();
        }

        protected void butSearch_Click(object sender, EventArgs e)
        {
            LoadData(true);
        }

        protected void aspNetPager_PageChanged(object sender, EventArgs e)
        {
            LoadData(true);
        }

        protected void rptLine_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                switch (e.CommandName)
                {
                    case "Delete":
                        if (LineBLL.DeleteLine(int.Parse(e.CommandArgument.ToString())) > 0)
                        {
                            Message.Msg(this.Page, CS.MsgDelSuccess, MessageType.Success);
                        }
                        else
                        {
                            Message.Msg(this.Page, CS.MsgDelFailure, MessageType.Failure);
                        }
                        break;
                }
                LoadData(true);
            }
        }
    }
}